<?php
include_once("includes/global.php");
include_once("includes/smarty_config.php");
include_once("$config[webroot]/module/product/includes/plugin_order_class.php");
require_once($config['webroot']."/module/payment/lib/wechatpay/lib/WxPay.Api.php");
$raw_xml = file_get_contents("php://input");
$notify = new \WxPayNotifyCallBack();
$notify->Handle(false);
$res = $notify->GetValues();
$order=new order();

if ($res['return_code'] === "SUCCESS" && $res['return_msg'] === "OK") {
    libxml_disable_entity_loader(true);
    $ret = json_decode(json_encode(simplexml_load_string($raw_xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    $order_no = $ret['out_trade_no'];
    $pay_free = $ret['total_fee']/100;

    $o_sql = "select * from ".ORDER." where order_id = '$order_no' ";
    $db->query($o_sql);
    $order_info = $db->fetchRow();
    $total_free = $order_info['cost'] + $order_info['freight'];
//    if($pay_free != $total_free ){
//        //验证 支付 金额 和 订单 金额 是否一致
//        msg("main.php");
//        return false;
//    }
    $sql="update ".ORDER." set status='2',pay_status='2',pay_time=".time()." where order_id='$order_no'";
    $db->query($sql);
    $order->set_order_statu($order_no,2);
    set_seat_order($order_no);

    //购票成功 发送短信
    $o_sql_p = "select * from ".ORPRO." where order_id = '$order_no' ";
    $db->query($o_sql_p);
    $p_list = $db->getRows();

    foreach($p_list as $key => $val){
        //减少库存
        if(!empty($val['num'])){
            $u_sql = "update ".SETMEAL." set stock=stock-$val[num] where id =$val[product_id] ";
            $this->db->query($u_sql);
        }
        $sms_msg = '【海峡票务】您已成功支付'.$val['name']."演出时间 ".date("Y-m-d H:i:s",$val['per_date']).
            " 演出票价 ".$val['price']."”门票".$val['num']."张。我们将尽快处理您的订单。如有问题请联系海峡客服：4000-114-978。";
        $res = jh_send_sms($_POST[ship_mobile],$sms_msg,2);
    }


}
?>